from lxml import etree
import os

def update_file(dir_name):
    files = os.listdir(dir_name)
    new_path = "D:/资料/CV/video/dataset/new_xml/"

    for file_name in files:
        with open(dir_name+file_name, encoding='UTF-8') as fid:
            xml_str = fid.read()
            if 'trolley casew' in xml_str:
                print(file_name)
                new_xml_str = xml_str.replace('trolley casew', 'trolley case')

        with open(new_path+file_name, 'w', encoding='UTF-8') as fid:
            fid.write(new_xml_str)




def count_category():
    dir_name = 'D:/资料/CV/video/dataset/xml/'
    files = os.listdir(dir_name)
    backpack_count, satchel_count, trolley_case_count, tote_bag_count = 0,0,0,0
    for file_name in files:
        with open(dir_name+file_name, encoding='UTF-8') as fid:
            xml_str = fid.read()
            backpack_count += xml_str.count("backpack")
            satchel_count += xml_str.count("satchel")
            trolley_case_count += xml_str.count("trolley case")
            tote_bag_count += xml_str.count("tote bag")

    print(backpack_count,satchel_count,trolley_case_count,tote_bag_count)

"5223 4349 1534 3555"
"7675 7123 1564 5070"

if __name__ == '__main__':
    count_category()

